define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/apply/index',
                    add_url: 'member/apply/add',
                    edit_url: 'member/apply/edit',
                    del_url: 'member/apply/del',
                    multi_url: 'member/apply/multi',
                    table: 'applypartner',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'applysn', title: __('Applysn')},
                        {field: 'type', title: __('Type'), searchList: {"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'username', title: __('Username')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'audit', title: __('Audit'), searchList: {"0":__('Audit 0'),"1":__('Audit 1'),"2":__('Audit 2')}, formatter: Table.api.formatter.normal},
                        {field: 'applytime', title: __('Applytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'audittime', title: __('Audittime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'member.nickname', title: __('Member.nickname')},
                        // {field: 'id', title: __('Id')},
                        // {field: 'applysn', title: __('Applysn')},
                        // {field: 'recomopenid', title: __('Recomopenid')},
                        // {field: 'openid', title: __('Openid')},
                        // {field: 'refereeid', title: __('Refereeid')},
                        // {field: 'type', title: __('Type'), searchList: {"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        // {field: 'mobile', title: __('Mobile')},
                        // {field: 'username', title: __('Username')},
                        // {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        // {field: 'audit', title: __('Audit'), searchList: {"0":__('Audit 0'),"1":__('Audit 1'),"2":__('Audit 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'applytime', title: __('Applytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'audittime', title: __('Audittime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'member.id', title: __('Member.id')},
                        // {field: 'member.openid', title: __('Member.openid')},
                        // {field: 'member.avatar_image', title: __('Member.avatar_image'), formatter: Table.api.formatter.image},
                        // {field: 'member.username', title: __('Member.username')},
                        // {field: 'member.nickname', title: __('Member.nickname')},
                        // {field: 'member.mobile', title: __('Member.mobile')},
                        // {field: 'member.agentid', title: __('Member.agentid')},
                        // {field: 'member.identity', title: __('Member.identity')},
                        // {field: 'member.membersn', title: __('Member.membersn')},
                        // {field: 'member.p_id', title: __('Member.p_id')},
                        // {field: 'member.credit', title: __('Member.credit'), operate:'BETWEEN'},
                        // {field: 'member.creditcode', title: __('Member.creditcode')},
                        // {field: 'member.bindtime', title: __('Member.bindtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'member.periodtime', title: __('Member.periodtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'member.createtime', title: __('Member.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table,
                            //在原有按钮的基础上增加一个 增加多个在buttons中添加即可
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('通过'),
                                    title: __('通过'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-folder-o',
                                    url: '/admin/member/Apply/pass',
                                    confirm: '请谨慎操作!!!确认通过吗?',
                                    success: function (data, ret) {
                                        console.log('success ret:',ret);
                                        if(ret.code == 1){
                                            Layer.alert('审核通过');
                                            window.location ='';
                                            return false;
                                        }else{
                                            Layer.alert(ret.msg);
                                            return false;
                                        }
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error:function (data,ret) {
                                        console.log('error ret:',ret)
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('驳回'),
                                    title: __('驳回'),
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    icon: 'fa fa-folder-o',
                                    url: '/admin/member/Apply/reject',
                                    confirm: '请谨慎操作!!!确认驳回?',
                                    success: function (data, ret) {
                                        if(ret.code == 1){
                                            Layer.alert('驳回成功');
                                            window.location.reload();
                                            return false;
                                        }else{
                                            Layer.alert(ret.msg);
                                            return false;
                                        }
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value,row,index) {
                                var that = $.extend({},this);
                                var table = $(that.table).clone(true);
                                //控制是否显示
                                if((row.status !=3 &&  row.status !=1) || row.audit !=0 ){
                                    $(table).data('operate-ajax',null); //隐藏
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index);
                            }
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});